package per.turtle.system.controller;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import per.turtle.common.core.web.controller.BaseController;
import per.turtle.common.core.web.entity.CommonResult;
import per.turtle.common.core.web.page.TableDataInfo;
import per.turtle.common.excel.annotation.ResponseExcel;
import per.turtle.common.log.annotation.Log;
import per.turtle.common.log.enums.BusinessType;
import per.turtle.common.security.annotation.PreAuthorize;
import per.turtle.system.entity.SysDictData;
import per.turtle.system.service.ISysDictDataService;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author turtle
 */
@AllArgsConstructor
@Api(value = "dictData", tags = {"dictData_数据字典信息管理模块"})
@RequestMapping("dict/data")
@RestController
public class SysDictDataController extends BaseController {

    private final ISysDictDataService dictDataService;

    @ApiOperation(value = "新增字典数据信息", notes = "新增字典数据信息")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping("add")
    @PreAuthorize(hasPermission = "system:dict:add")
    public CommonResult<Void> add(@Validated @RequestBody SysDictData dict) {
        return result(dictDataService.insertDictData(dict) != null);
    }

    @ApiOperation(value = "根据字典类型查询字典数据信息", notes = "根据字典类型查询字典数据信息")
    @GetMapping("type/{dictType}")
    @PreAuthorize(hasPermission = "system:dict:query")
    public CommonResult<List<SysDictData>> dictType(@PathVariable String dictType) {
        List<SysDictData> data = dictDataService.selectDictDataByType(dictType);
        if (BeanUtil.isEmpty(data)) {
            data = new ArrayList<>();
        }
        return CommonResult.success(data);
    }

    @ApiOperation(value = "修改保存字典数据信息", notes = "修改保存字典数据信息")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping("edit")
    @PreAuthorize(hasPermission = "system:dict:edit")
    public CommonResult<Void> edit(@Validated @RequestBody SysDictData dict) {
        return result(dictDataService.updateDictData(dict) != null);
    }

    @ApiOperation(value = "导出数据字典信息", notes = "导出获取数据字典信息")
    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @PostMapping("export")
    @PreAuthorize(hasPermission = "system:dict:export")
    @ResponseExcel("字典数据")
    public List<SysDictData> export(HttpServletResponse response, @RequestBody SysDictData dictData) {
        return dictDataService.selectDictDataList(dictData);
    }

    @ApiOperation(value = "查询字典数据详细", notes = "查询字典数据详细")
    @GetMapping("{dictCode}")
    @PreAuthorize(hasPermission = "system:dict:query")
    public CommonResult<SysDictData> getInfo(@PathVariable Long dictCode) {
        return CommonResult.success(dictDataService.getById(dictCode));
    }

    @ApiOperation(value = "获取数据字典信息列表或分页", notes = "获取数据字典信息列表或分页（区别于是否传入分页参数）")
    @PostMapping("list")
    @PreAuthorize(hasPermission = "system:dict:list")
    public TableDataInfo<SysDictData> list(@RequestBody SysDictData dictData) {
        List<SysDictData> list = startPage(s -> dictDataService.selectDictDataList(dictData));
        return getDataTable(list);
    }

    @ApiOperation(value = "删除字典数据信息", notes = "删除字典数据信息")
    @DeleteMapping("{dictCodes}")
    @Log(title = "字典数据", businessType = BusinessType.DELETE)
    @PreAuthorize(hasPermission = "system:dict:remove")
    public CommonResult<Void> remove(@PathVariable Long[] dictCodes) {
        for (Long dictCode : dictCodes) {
            dictDataService.deleteDictDataById(dictCode);
        }
        return CommonResult.success();
    }

}
